package com.elite.www;

import cn.kmpro.util.KmproSign;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class callSign extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(callSign.class);

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String retString = "";

        String inputParam = null;
        InputStream inputStream = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String tempString = null;
        StringBuffer buffer = new StringBuffer();

        while ((tempString = reader.readLine()) != null) {
            buffer.append(tempString);
        }
        inputParam = buffer.toString();

        log.info("StartServlet接收调用报文信息：" + System.getProperty("line.separator") + inputParam);

        JSONObject jsonRs = null;
        String jsonKey = null;
        String jsonLoginname = null;
        try {
            jsonRs = JSONObject.fromObject(inputParam);
            jsonKey = jsonRs.getString("key");
            jsonLoginname = jsonRs.getString("loginname");
            log.debug("StartServlet 解析调用报文(key):" + System.getProperty("line.separator") + jsonKey);
            log.debug("StartServlet 返回调用报文(loginname):" + System.getProperty("line.separator") + jsonLoginname);

            if ((jsonKey != null) && (jsonKey.length() > 0)) {
                if ((jsonLoginname != null) && (jsonLoginname.length() > 0)) {
                    String sign = KmproSign.sign(jsonLoginname, jsonKey);

                    log.debug("StartServlet 生成签名(sign):" + sign);

                    retString = "{\"resultcode\":\"0\",\"resultmessage\":\"\",\"data\":{\"sign\":\"" + sign + "\"}}";
                } else {
                    retString = "{\"resultcode\":\"1\",\"resultmessage\":\"调用信息key值有误\",\"data\":{\"sign\":null}}";
                }
            } else
                retString = "{\"resultcode\":\"1\",\"resultmessage\":\"调用信息loginname值有误\",\"data\":{\"sign\":null}}";
        } catch (Exception e) {
            retString = "{\"resultcode\":\"1\",\"resultmessage\":\"处理签名异常\",\"data\":{\"sign\":null}}";
            log.error("StartServlet doPost Exception:" + System.getProperty("line.separator") + e.getMessage());
        }

        log.debug("StartServlet 返回调用报文信息:" + System.getProperty("line.separator") + retString);

        out.println(retString);
        out.flush();
        out.close();
    }
}